package com.uex.dev.manage.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import com.uex.dev.manage.admin.dao.ProjectVersionDao;
import com.uex.dev.manage.admin.po.ProjectPO;
import com.uex.dev.manage.admin.po.ProjectVersionPO;
import com.uex.dev.manage.admin.service.IProjectService;
import com.uex.dev.manage.admin.service.IProjectVersionService;
import com.uex.dev.manage.admin.utils.LogUtil;
import com.uex.dev.manage.admin.vo.project.ProjectVerListResp;
import com.uex.dev.manage.admin.vo.project.ProjectVersionResp;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 项目版本 服务实现类
 * </p>
 *
 * @author wangchao
 * @since 2024-02-26
 */
@Service
public class ProjectVersionServiceImpl extends ServiceImpl<ProjectVersionDao, ProjectVersionPO> implements IProjectVersionService {

    @Resource
    private IProjectService projectService;

    @Override
    public ProjectVersionResp getPrjVersion(Integer prjId, Integer id) {
        Preconditions.checkArgument(ObjectUtils.isNotNull(projectService.getById(prjId)), "项目Id不存在");
        ProjectVersionResp projectVersionResp = new ProjectVersionResp();
        LambdaQueryWrapper<ProjectVersionPO> wrapper = Wrappers.<ProjectVersionPO>lambdaQuery()
                .eq(ProjectVersionPO::getVerPrjid, prjId)
                .eq(ObjectUtils.isNotNull(id) && id != 0, ProjectVersionPO::getId, id)
                .orderByDesc(ProjectVersionPO::getId)
                .last("Limit 1");
        ProjectVersionPO projectVersion = this.getOne(wrapper);
        Preconditions.checkArgument(ObjectUtils.isNotNull(projectVersion), "项目版本信息不存在");
        BeanUtils.copyProperties(projectVersion, projectVersionResp);
        LogUtil.log(0, "获取项目版本");
        return projectVersionResp;
    }

    @Override
    public List<ProjectVerListResp> getPrjVerList(Integer prjId) {
        LambdaQueryWrapper<ProjectVersionPO> wrapper = Wrappers.<ProjectVersionPO>lambdaQuery()
                .eq(ProjectVersionPO::getVerPrjid, prjId)
                .orderByDesc(ProjectVersionPO::getId);
        List<ProjectVersionPO> versionList = this.list(wrapper);
        Preconditions.checkArgument(!versionList.isEmpty(), "项目Id不存在");
        ArrayList<ProjectVerListResp> resultList = new ArrayList<>();
        versionList.forEach(version -> {
            ProjectVerListResp projectVerListResp = ProjectVerListResp.fromPO(version);
            resultList.add(projectVerListResp);
        });
        LogUtil.log(0, "获取项目版本列表");
        return resultList;
    }

    @Override
    public void updatePrjVersion(ProjectVersionPO projectVerUpdateReq) {
        if (projectVerUpdateReq.getId() == null || projectVerUpdateReq.getId() == 0) {
            Preconditions.checkArgument(ObjectUtils.isNotNull(projectVerUpdateReq.getVerPrjid()), "项目Id不能为空");
            Preconditions.checkArgument(ObjectUtils.isNotNull(projectVerUpdateReq.getVerTime()), "版本更新时间不能为空");
            ProjectPO project = projectService.getById(projectVerUpdateReq.getVerPrjid());
            Preconditions.checkArgument(ObjectUtils.isNotNull(project), "项目不存在");
            this.save(projectVerUpdateReq);
            LogUtil.log(0, "添加项目版本");
        } else {
            ProjectPO project = projectService.getById(projectVerUpdateReq.getVerPrjid());
            Preconditions.checkArgument(Objects.nonNull(project), "项目不存在");
            Preconditions.checkArgument(this.updateById(projectVerUpdateReq), "项目版本信息不存在");
            LogUtil.log(0, "更新项目版本");
        }
    }
}
